﻿using _003_MVVM的Messenger.Message;
using CommunityToolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace _003_MVVM的Messenger
{
    /// <summary>
    /// RecWindow.xaml 的交互逻辑
    /// </summary>
    public partial class RecWindow : Window, IRecipient<UiMessage>
    {
        public RecWindow()
        {
            InitializeComponent();

            //订阅消息
            //WeakReferenceMessenger.Default.Register<UiMessage>(this, (r, m) =>
            //{

            //});

            WeakReferenceMessenger.Default.Register<UiMessage>(this);

            WeakReferenceMessenger.Default.Register<Ui2Message>(this, (r, m) =>
            {
                m.Reply("From RecvWindow");
            });
        }

        void IRecipient<UiMessage>.Receive(UiMessage message)
        {
            tbMsg.Text = message.Value;
        }

        private void Window_Unloaded(object sender, RoutedEventArgs e)
        {
           WeakReferenceMessenger.Default.Unregister<UiMessage>(this);
        }
    }
}
